package com.bai.cms.controller;


import com.bai.cms.entity.CrmBanner;
import com.bai.cms.service.CrmBannerService;
import com.bai.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author bai
 * @since 2023-08-10
 */
@RestController
@RequestMapping("/cms/banneradmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    //TODO:分页查询
    @ApiOperation("分页查询")
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit) {
        Page<CrmBanner> crmBannerPage = new Page<>(page, limit);
        crmBannerService.page(crmBannerPage, null);
        return R.ok().data("items", crmBannerPage.getRecords()).data("total", crmBannerPage.getTotal());
    }

    //TODO：添加
    @PostMapping("addBanner")
    @ApiOperation("添加")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //TODO:修改
    @ApiOperation("修改")
    @PutMapping("update")
    public R update(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

    //TODO:删除
    @ApiOperation("删除")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }

    //TODO:根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("item",crmBanner);
    }
}

